package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.AiRecordingDuan;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.AiRecordingMain;
import com.ruoyi.system.service.IAiRecordingMainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 录音文件上传记录Controller
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/system/aimain")
public class AiRecordingMainController extends BaseController
{
    @Autowired
    private IAiRecordingMainService aiRecordingMainService;

    /**
     * 录音文件上传----单个文件上传
     */
    @Log(title = "录音文件上传", businessType = BusinessType.OTHER)
    @PostMapping("/fileUpload")
    public AjaxResult avatar( @RequestParam("file") MultipartFile file  , @RequestParam("language") String language) throws Exception
    {
//
//       String language= parameter.get("language").toString();
//       String  language="en";
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.uploadBig(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.MEDIA_WAV);
//            avatar.replace("/profile/", "/");
            return toAjax(aiRecordingMainService.insertAiRecordingMainFile(avatar.replace("/profile/", "/"),avatar,language,"true",null,null));
        }
        return error("上传文件异常，请联系管理员");
    }








    /**
     * 录音文件上传---多个文件用界面数据上传
     */
    @Log(title = "录音文件上传", businessType = BusinessType.OTHER)
    @PostMapping("/fileUploadBatch")
    public AjaxResult fileUploadBatch( @RequestBody AiRecordingMain aiRecordingMain) throws Exception
    {
       String  language=aiRecordingMain.getLanguages();
       String  segmented=aiRecordingMain.getSegmented();
        aiRecordingMain.getFilePathList().forEach(file->{
            if (!file.isEmpty())
            {
                try {
                    aiRecordingMainService.insertAiRecordingMainFile(file.replace("/profile/", "/"),file,language,segmented,null,null);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return toAjax(1);
    }

    @Log(title = "录音文件上传", businessType = BusinessType.OTHER)
    @PostMapping("/fileUploadBatchFast")
    public AjaxResult fileUploadBatchFast( @RequestBody AiRecordingMain aiRecordingMain) throws Exception
    {
        String  language=aiRecordingMain.getLanguages();
        String  segmented="false";
        Long customerId=aiRecordingMain.getCustomerId();
        Date time=aiRecordingMain.getTime();
        aiRecordingMain.getFilePathList().forEach(file->{
            if (!file.isEmpty())
            {
                try {
                    aiRecordingMainService.insertAiRecordingMainFile(file.replace("/profile/", "/"),file,language,segmented,customerId,time);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return toAjax(1);
    }


    /**
     * 查询管理员权限
     * @return
     */
    @GetMapping("/selectPer")
    public AjaxResult selectPer()
    {
        if (SecurityUtils.getUserId()!=1L) {
            return AjaxResult.success("false");
        }else{
            return AjaxResult.success("true");
        }
    }
    /**
     * 查询管理员权限   3和6都有权限，重新解析
     * @return
     */
    @PutMapping("/updateMainDate")
    public AjaxResult updateMainDate( @RequestBody AiRecordingMain aiRecordingMain) throws Exception
    {
        return       aiRecordingMainService.updateMainDate(aiRecordingMain);
    }
    /**
     * 只有6才可以，重新生成最后的结论，
     * @return
     */
    @PutMapping("/updateMainDateCustomerName")
    public AjaxResult updateMainDateCustomerName( @RequestBody AiRecordingDuan aiRecordingDuan) throws Exception
    {
        return       aiRecordingMainService.updateMainDateCustomerName(aiRecordingDuan);
    }


    @PostMapping("/removeDuan")
    public AjaxResult removeDuan(@RequestBody AiRecordingDuan aiRecordingDuan)
    {
        return aiRecordingMainService.deleteRemoveDuan(aiRecordingDuan);
    }
    @PostMapping("/removeMain")
    public AjaxResult removeMain(@RequestBody AiRecordingDuan aiRecordingDuan)
    {
        return aiRecordingMainService.deleteRemoveMain(aiRecordingDuan);
    }



    @Log(title = "进行deepseek分段", businessType = BusinessType.OTHER)
    @PostMapping("/segmented")
    public AjaxResult segmented( @RequestBody AiRecordingMain aiRecordingMain) throws Exception
    {
        return   aiRecordingMainService.segmentedByDeepSeek(aiRecordingMain);
    }


    /**
     * 上传数据到deepseek去解析数据
     */
    @Log(title = "上传数据到deepseek去解析数据", businessType = BusinessType.OTHER)
    @PostMapping("/deepseekParse")
    public AjaxResult deepseekParse(@RequestBody AiRecordingDuan aiRecordingDuan) throws Exception {
        return aiRecordingMainService.deepseekParse(aiRecordingDuan);
    }

    /**
     * 查询录音文件上传记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:main:list')")
    @GetMapping("/selectCustomerTime")
    public AjaxResult selectCustomerTime()
    {
        List<AiRecordingDuan> list = aiRecordingMainService.selectCustomerTime();
        return AjaxResult.success(list);
    }

//    回调地址
//    订单完成时回调该地址通知完成支持get 请求，我们会在回调地址中拼接参数，长度限制512： http://{ip}/{port}?xxx&OrderId=xxxx&status=1
//    参数：orderId为订单号、status为订单状态: 1(转写识别成功) 、-1(转写识别失败)
//
    @GetMapping("/getXunFeiResult")
    public void getXunFeiResult(@RequestBody AiRecordingMain aiRecordingMain) throws Exception {
       aiRecordingMainService.getXunFeiResult(aiRecordingMain);

    }



    @GetMapping("/selectGroupTimeNo")
    public AjaxResult selectGroupTimeNo()
    {
        AiRecordingDuan aiRecordingDuan=new AiRecordingDuan();
//        aiRecordingDuan.setUserId(SecurityUtils.getUserId());
        List<AiRecordingDuan> list = aiRecordingMainService.selectGroupTime(aiRecordingDuan);
        return AjaxResult.success(list);
    }
    @GetMapping("/selectGroupTimeHave")
    public AjaxResult selectGroupTimeHave()
    {
        AiRecordingDuan aiRecordingDuan=new AiRecordingDuan();
        aiRecordingDuan.setUserId(SecurityUtils.getUserId());
        List<AiRecordingDuan> list = aiRecordingMainService.selectGroupTime(aiRecordingDuan);
        return AjaxResult.success(list);
    }
    @GetMapping("/selectGroupPerson")
    public AjaxResult selectGroupPerson()
    {
        List<AiRecordingDuan> list = aiRecordingMainService.selectGroupPerson();
        return AjaxResult.success(list);
    }

//    public List<AiRecordingDuan> selectCustomerTime(AiRecordingDuan aiRecordingDuan);

    /**
     * 查询录音文件上传记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(AiRecordingMain aiRecordingMain)
    {
        startPage();
//        aiRecordingMain.setUserId(SecurityUtils.getUserId());
//        aiRecordingMain.setDeptId(SecurityUtils.getDeptId());
        List<AiRecordingMain> list = aiRecordingMainService.selectAiRecordingMainList(aiRecordingMain);

        return getDataTable(list);
    }

    /**
     * 导出录音文件上传记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:main:export')")
    @Log(title = "录音文件上传记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiRecordingMain aiRecordingMain)
    {
        List<AiRecordingMain> list = aiRecordingMainService.selectAiRecordingMainList(aiRecordingMain);
        ExcelUtil<AiRecordingMain> util = new ExcelUtil<AiRecordingMain>(AiRecordingMain.class);
        util.exportExcel(response, list, "录音文件上传记录数据");
    }

    /**
     * 获取录音文件上传记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aiRecordingMainService.selectAiRecordingMainById(id));
    }

    /**
     * 新增录音文件上传记录
     */
//    @PreAuthorize("@ss.hasPermi('system:main:add')")
    @Log(title = "录音文件上传记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiRecordingMain aiRecordingMain)
    {
        return toAjax(aiRecordingMainService.insertAiRecordingMain(aiRecordingMain));
    }

    /**
     * 修改录音文件上传记录
     */
//    @PreAuthorize("@ss.hasPermi('system:main:edit')")
    @Log(title = "录音文件上传记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiRecordingMain aiRecordingMain)
    {
        return toAjax(aiRecordingMainService.updateAiRecordingMain(aiRecordingMain));
    }

    /**
     * 删除录音文件上传记录
     */
//    @PreAuthorize("@ss.hasPermi('system:main:remove')")
    @Log(title = "录音文件上传记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aiRecordingMainService.deleteAiRecordingMainByIds(ids));
    }
}
